/*
 * Copyright (c) 2016-2017 fulihui.com All right reserved
 * Project: information-core-170307
 * File name: DynamicNavbarRepositoryImpl.java
 */

package com.fulihui.information.core.repository.impl;

import com.fulihui.information.biz.conv.DynamicNavbarConv;
import com.fulihui.information.core.repository.DynamicNavbarRepository;
import com.fulihui.information.dal.dataobj.InfoDynamicNavbar;
import com.fulihui.information.dal.dataobj.InfoDynamicNavbarExample;
import com.fulihui.information.dal.dataobj.InfoDynamicNavbarKey;
import com.fulihui.information.dal.mapper.InfoDynamicNavbarMapper;
import com.fulihui.information.dto.DynamicNavbarDTO;
import com.fulihui.information.request.DynamicNavbarQueryPageRequest;
import com.fulihui.information.request.DynamicNavbarSaveRequest;
import org.near.toolkit.common.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

import static com.fulihui.information.util.VersionFormat.atoi;
import static org.springframework.util.CollectionUtils.isEmpty;

/**
 * @author Willard.Hu on 2017/3/3 0003.
 */
@Component
public class DynamicNavbarRepositoryImpl implements DynamicNavbarRepository {
    @Autowired
    private InfoDynamicNavbarMapper infoDynamicNavbarMapper;

    /**
     * @see DynamicNavbarRepository#insert(DynamicNavbarSaveRequest)
     */
    @Override
    public void insert(DynamicNavbarSaveRequest request) {
        InfoDynamicNavbar record = DynamicNavbarConv.toDataObj(request);
        infoDynamicNavbarMapper.insertSelective(record);
    }

    /**
     * @see DynamicNavbarRepository#update(DynamicNavbarSaveRequest)
     */
    @Override
    public boolean update(DynamicNavbarSaveRequest request) {
        InfoDynamicNavbar record = DynamicNavbarConv.toDataObj(request);
        return infoDynamicNavbarMapper.updateByPrimaryKeySelective(record) > 0;
    }

    /**
     * @see DynamicNavbarRepository#delete(InfoDynamicNavbarKey)
     */
    @Override
    public boolean delete(InfoDynamicNavbarKey key) {
        return infoDynamicNavbarMapper.deleteByPrimaryKey(key) > 0;
    }

    /**
     * @see DynamicNavbarRepository#querySingle(InfoDynamicNavbarKey)
     */
    @Override
    public DynamicNavbarDTO querySingle(InfoDynamicNavbarKey key) {
        InfoDynamicNavbar record = infoDynamicNavbarMapper.selectByPrimaryKey(key);
        return DynamicNavbarConv.toDTO(record);
    }

    /**
     * @see DynamicNavbarRepository#queryPage(DynamicNavbarQueryPageRequest)
     */
    @Override
    public List<DynamicNavbarDTO> queryPage(DynamicNavbarQueryPageRequest request) {
        InfoDynamicNavbarExample example = new InfoDynamicNavbarExample();
        InfoDynamicNavbarExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(request.getStartVersion())) {
            criteria.andVersionGreaterThanOrEqualTo(atoi(request.getStartVersion()));
        }
        if (StringUtil.isNotBlank(request.getEndVersion())) {
            criteria.andVersionLessThan(atoi(request.getEndVersion()));
        }
        if (StringUtil.isNotBlank(request.getOsType())) {
            criteria.andOsTypeEqualTo(request.getOsType());
        }
        example.setOffset(request.start4Mysql());
        example.setLimit(request.getRows());
        List<InfoDynamicNavbar> list = infoDynamicNavbarMapper.selectByExample(example);
        return DynamicNavbarConv.toList(list);
    }

    /**
     * @see DynamicNavbarRepository#queryPreviousVersion(String, int)
     */
    @Override
    public DynamicNavbarDTO queryPreviousVersion(String osType, int version) {
        InfoDynamicNavbarExample example = new InfoDynamicNavbarExample();
        example.createCriteria().andOsTypeEqualTo(osType)
                .andVersionLessThan(version);
        example.setOrderByClause("version desc");
        example.setLimit(1);
        List<InfoDynamicNavbar> list = infoDynamicNavbarMapper.selectByExample(example);
        return isEmpty(list) ? null : DynamicNavbarConv.toDTO(list.get(0));
    }

    @Override
    public List<DynamicNavbarDTO> query(InfoDynamicNavbarExample example) {
        List<InfoDynamicNavbar> list = infoDynamicNavbarMapper.selectByExample(example);
        return DynamicNavbarConv.toList(list);
    }

    /**
     * @see DynamicNavbarRepository#totalCount(DynamicNavbarQueryPageRequest)
     */
    @Override
    public int totalCount(DynamicNavbarQueryPageRequest request) {
        InfoDynamicNavbarExample example = new InfoDynamicNavbarExample();
        InfoDynamicNavbarExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(request.getStartVersion())) {
            criteria.andVersionGreaterThanOrEqualTo(atoi(request.getStartVersion()));
        }
        if (StringUtil.isNotBlank(request.getEndVersion())) {
            criteria.andVersionLessThan(atoi(request.getEndVersion()));
        }
        if (StringUtil.isNotBlank(request.getOsType())) {
            criteria.andOsTypeEqualTo(request.getOsType());
        }
        return (int) infoDynamicNavbarMapper.countByExample(example);
    }
}
